package asn.match.dao;

import java.util.List;

import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import asn.match.model.InternalMessage;

@Repository("internal_message")
@PersistenceContext(type = PersistenceContextType.EXTENDED)
@Transactional
public class InternalMessageDaoImpl implements InternalMessageDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void addInternalMessage(InternalMessage internalMessage) {
		this.sessionFactory.getCurrentSession().save(internalMessage);
	}

	@Override
	public void updateInternalMessage(InternalMessage internalMessage) {
		this.sessionFactory.getCurrentSession().saveOrUpdate(internalMessage);
	}

	@Override
	public void deleteInternalMessage(InternalMessage internalMessage) {
		this.sessionFactory.getCurrentSession().delete(internalMessage);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<InternalMessage> findAllInternalMessages() {
		return (List<InternalMessage>) this.sessionFactory.getCurrentSession().createQuery("from InternalMessage internal_message").list();
	}

	@Override
	public InternalMessage findInternalMessageById(Integer internalMessageId) {
		return (InternalMessage) this.sessionFactory.getCurrentSession().get(InternalMessage.class, internalMessageId);
	}
}
